import sys
import re
class CountWord:
	@staticmethod
	def Count(lines):
		wordcount = 0
		wordStatistical = {}
		restr = r'^[a-zA-Z][a-zA-Z0-9]*$'
		for line in lines:
			if line.strip():
				words = re.split(r'\W',line)
				#words =  line.split(' ')
				for word in words:
					if re.match(restr,word):
						wordcount = wordcount + 1
						_word = word.replace("\n", "").lower()
						if not _word in wordStatistical:
							wordStatistical[_word] = 1
						else:
							wordStatistical[_word] = wordStatistical[_word] + 1
		keys = CountWord.SortByValue(wordStatistical)
		rtn= {}
		rtn["count"] = wordcount
		rtn["dict"] = wordStatistical
		rtn["keys"] = keys
		return rtn
	
	@staticmethod
	def SortByValue(d): 
		items=d.items() 
		backitems=[[v[1],v[0]] for v in items] 
		backitems.sort(reverse=True) 
		return [ backitems[i][1] for i in range(0,len(backitems))] 